<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索框区域 -->
      <el-row>
        <el-col :span="10">
          <el-input placeholder="请输入内容">
          <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- table数据列表区域 -->
       <el-table :data="ordersList" style="width: 100%" border stripe>
         <!-- 关于鼠标向下选择的快捷键：Ctrl+D -->
          <el-table-column type="index"></el-table-column>
          <el-table-column prop="order_number" label="订单编号"></el-table-column>
          <el-table-column prop="order_price" label="订单价格"></el-table-column>
          <el-table-column prop="pay_status" label="是否支付">
            <template slot-scope="scope">
              <el-tag type="primary" v-if="scope.row.pay_status === '0'">未支付</el-tag>
              <el-tag type="warning" v-else>已支付</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="is_send" label="是否发货"></el-table-column>
          <el-table-column prop="create_time" label="下单时间"></el-table-column>
          <el-table-column label="操作">
            <template>
              <el-button size="mini" type="success" icon="el-icon-edit" @click="editAddress"></el-button>
               <el-button size="mini" type="danger" icon="el-icon-location" @click="processOrder"></el-button>
            </template>
          </el-table-column>
       </el-table>
      <!-- 底部分页区域 -->
       <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="this.queryInfo.pagenum"
          :page-sizes="[1, 2, 3, 4, 5, 10]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="this.total">
       </el-pagination>
    </el-card>
    <!-- 修改地址对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="editAddressDialogVisible"
      width="50%"
      @close="editAddressClosed">
      <el-form :model="editAddressForm" :rules="editAddressRules" ref="editAddressRefs" label-width="100px" class="demo-ruleForm">
      <el-form-item label="省市区/县" prop="name">
        <!-- 级联选择器 -->
        <el-cascader :options="cityData" v-model="editAddressForm.name"></el-cascader>
      </el-form-item>
      <el-form-item label="详细地址" prop="addressName">
        <el-input v-model="editAddressForm.addressName"></el-input>
      </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editAddressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editAddressSubmit">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 显示物流的对话框 -->
     <el-dialog
      title="物流进度"
      :visible.sync="showProcessDialogVisible"
      width="50%">
      <!-- 显示物流时间线 -->
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in showProcess"
          :key="index"
          :timestamp="activity.time">
          {{activity.context}}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
// 将省市区级联选择器需要的数据导入
import cityData from './citydata.js'
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 订单总数据条数
      total: 0,
      // 订单数据列表
      ordersList: [],
      // 控制修改地址的对话框的显示与隐藏
      editAddressDialogVisible: false,
      // 省市区级获取需要的数据
      cityData: cityData,
      // 修改地址对话框绑定的数据
      editAddressForm: {
        name: [],
        addressName: ''
      },
      // 修改地址对话框对应的规则
      editAddressRules: {
        name: [
          { required: true, message: '请选择省市区/县', trigger: 'change' }
        ],
        addressName: [
          { required: true, message: '请选择详细地址', trigger: 'blur' }
        ]
      },
      // 控制物流进度对话框的显示与隐藏
      showProcessDialogVisible: false,
      // 请求物流信息接口出错，禁用一下数据暂代物流查询出来的结果列表
      showProcess: [
        {
          time: '2018-05-10 09:39:00',
          ftime: '2018-05-10 09:39:00',
          context: '已签收,感谢使用顺丰,期待再次为您服务',
          location: ''
        },
        {
          time: '2018-05-10 08:23:00',
          ftime: '2018-05-10 08:23:00',
          context: '[北京市]北京海淀育新小区营业点派件员 顺丰速运 95338正在为您派件',
          location: ''
        },
        {
          time: '2018-05-09 12:22:24',
          ftime: '2018-05-09 12:22:24',
          context: '您的订单将由HLA（北京海淀区清河中街店）门店安排发货。',
          location: ''
        },
        {
          time: '2018-05-08 21:36:04',
          ftime: '2018-05-08 21:36:04',
          context: '商品已经下单',
          location: ''
        }
      ]
    }
  },
  created() {
    // 获取订单列表数据
    this.getOrderList()
  },
  methods: {
    // 页面初加载获取点歌单列表数据的函数
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', { params: this.queryInfo })
      // console.log(res)
      // 判断返回的状态码
      if (res.meta.status !== 200) {
        return this.$message.error('订单管理数据列表获取失败')
      }
      this.total = res.data.total
      // 保存数据
      this.ordersList = res.data.goods
      console.log(this.ordersList)
      this.$message.success('订单管理数据列表获取成功')
    },
    // 页面数据显示总条数改变事件
    handleSizeChange(newSize) {
      // 将最新的size保存
      this.queryInfo.pagesize = newSize
      // 刷新数据列表
      this.getOrderList()
    },
    // 当前点击页改变触发事件
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    // 点击修改按钮触发的事件
    editAddress() {
      // 将修改按钮的控制属性设置为true
      this.editAddressDialogVisible = true
    },
    // 清空修改对话框事件
    editAddressClosed() {
      this.$refs.editAddressRefs.resetFields()
    },
    // 点击修改地址对话框确定按钮触发事件
    editAddressSubmit() {
      // 进行表单预校验
      this.$refs.editAddressRefs.validate(async valid => {
        if (!valid) return
        // 发送请求修改地址
        const { data: res } = await this.$http.get(`/kuaidi/${this.ordersList.order_id}`)
        console.log(res)
      })
    },
    // 这是点击查询物流按钮触发的事件
    async processOrder() {
      // 调用后台接口查询物流信息
      // 本次请求无法获取结果，也不发生报错，请求状态显示200和pending,预计可能是服务器或者网络出错
      // 后续物流时间先无法渲染
      // const { data: res } = await this.$http.get('/kuaidi/' + 1106975712662)
      // console.log(res)
      // console.log('ok')
      // 点击按钮，弹出物流状态的对话框
      this.showProcessDialogVisible = true
    }
  }
}
</script>

<style scoped>
/* 导入timelime时间线对应的样式表 */
@import '../../plugins/timeline/timeline.css';
@import '../../plugins/timeline-item/timeline-item.css';
.el-pagination {
  margin-top: 15px;
  margin-left: 15px;
}
.el-cascader  {
  width: 100%;
}
</style>
